package com.jstyle.jclife.activity;

import android.os.Bundle;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.text.TextUtils;
import android.view.View;
import android.widget.Button;
import android.widget.ImageView;

import com.jstyle.jclife.R;
import com.jstyle.jclife.adapter.CheckInAdapter;
import com.jstyle.jclife.app.MyApplication;
import com.jstyle.jclife.model.Device1810;
import com.jstyle.jclife.model.JstyleDevice;
import com.jstyle.jclife.utils.ScreenUtils;
import com.jstyle.jclife.utils.SharedPreferenceUtils;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;

import butterknife.BindArray;
import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.OnClick;

/**
 * 训练模式
 */
public class ExerciseModeActivity extends BaseActivity {

    @BindView(R.id.bt_goal_back)
    Button btGoalBack;
    @BindView(R.id.iv_back)
    ImageView ivBack;
    @BindView(R.id.iv_share)
    ImageView ivShare;
    @BindView(R.id.RecyclerView_exercise_mode)
    RecyclerView RecyclerViewExerciseMode;
    private CheckInAdapter settingAdapter;
    @BindArray(R.array.clock_mode)
     String []arraySetting;
    @BindArray(R.array.clock_mode_1810)
     String []arrayMode1810;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_exercise_mode);
        ButterKnife.bind(this);
        init();
    }

    private void init() {
        btGoalBack.setText(R.string.Check_in);
        ScreenUtils.setTitleTypeface(btGoalBack);
        ivShare.setVisibility(View.GONE);
        LinearLayoutManager linearLayoutManager = new LinearLayoutManager(this);
        linearLayoutManager.setOrientation(LinearLayoutManager.VERTICAL);
        String mode= SharedPreferenceUtils.getString(SharedPreferenceUtils.KEY_Exercise_Mode,"0,1,2,3");

        List<Integer>checkedList=new ArrayList<>();
        if(!TextUtils.isEmpty(mode)){
            String[]modeArray=mode.split(",");
           for(String s:modeArray){
               checkedList.add(Integer.valueOf(s));
           }
        }

        JstyleDevice jstyleDevice= MyApplication.getJstyleDevice();
        if(jstyleDevice instanceof Device1810){
            settingAdapter = new CheckInAdapter(Arrays.asList(arrayMode1810),checkedList);
        }else{
            settingAdapter = new CheckInAdapter(Arrays.asList(arraySetting),checkedList);
        }
        //settingAdapter = new CheckInAdapter(Arrays.asList(arraySetting),checkedList);
     //   settingAdapter.setOnItemClickListener(this);
        RecyclerViewExerciseMode.setAdapter(settingAdapter);
        RecyclerViewExerciseMode.setLayoutManager(linearLayoutManager);
    }

    @OnClick(R.id.iv_back)
    public void onViewClicked()
    {

        setResult(RESULT_OK);
        List<Integer>list=settingAdapter.getCheckedList();
        StringBuffer stringBuffer=new StringBuffer();
        for (int i=0;i<list.size();i++){
            stringBuffer.append(list.get(i)).append(i==list.size()-1?"":",");
        }
        SharedPreferenceUtils.setSpString(SharedPreferenceUtils.KEY_Exercise_Mode,stringBuffer.toString());
        finish();
    }

    @Override
    protected void onDestroy() {
        super.onDestroy();

    }
}
